package com.ceshiren.hogwarts.restassured.wechat.member;

import com.ceshiren.hogwarts.restassured.wechat.apiobject.MembersObject;
import com.ceshiren.hogwarts.restassured.wechat.apiobject.TokenHelper;
import com.ceshiren.hogwarts.restassured.wechat.task.EvnHelperTask;
import com.ceshiren.hogwarts.restassured.wechat.task.String2int;
import com.ceshiren.hogwarts.restassured.wechat.utils.User;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.qameta.allure.Description;
import io.qameta.allure.Story;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;


/**
 * @author Seven
 * @date 2020/12/23 18:27
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MembersTest {
    public static String accessToken;
    public static String uId;
    @BeforeAll
    public static void beforeAll(){
        accessToken = TokenHelper.getAccessToken();
        //清除成员数据
        EvnHelperTask.clearMembers(accessToken,"1");
    }

    @BeforeEach
    @AfterEach
    public void clearMembers(){

    }

    /**
     * 添加成员，使用csv进行数据驱动
     */
    @DisplayName("添加成员")
    @Description("测试是否添加成员成功")
    @Story("添加成员测试")
    @ParameterizedTest
    @CsvFileSource(resources = "/wechat/addmembers.csv", numLinesToSkip = 1)
//    @Execution(CONCURRENT)
    @Order(1)
    public void testAddMembers(String userId,String name,String mobile,@ConvertWith(String2int.class) int [] departmentIds){
        Map<String,Object> body = new HashMap<>();
        body.put("userid",userId);
        body.put("name",name);
        body.put("mobile",mobile);
        body.put("department",departmentIds);
        Response response = given().log().all()
                .contentType(ContentType.JSON)
                .queryParam("access_token",accessToken)
                .body(body)
                .when()
                .post("https://qyapi.weixin.qq.com/cgi-bin/user/create")
                .then().log().all().extract().response();
        assertEquals("0",response.path("errcode").toString());

    }


    /**
     * 查询成员，使用yaml文件进行数据驱动
     */
    @DisplayName("读取成员")
    @Description("测试是否正常查询指定成员")
    @Story("读取成员测试")
    @ParameterizedTest
    @MethodSource("getUserId")
    @Order(2)
    public void testListMembers(String userId){
        Response response = given().log().all()
                .contentType(ContentType.JSON)
                .queryParam("access_token",accessToken)
                .queryParam("userid",userId)
                .when()
                .get("https://qyapi.weixin.qq.com/cgi-bin/user/get")
                .then().log().all().extract().response();
        assertAll("查询返回值校验！",
                ()->assertEquals("0",response.path("errcode").toString()),
                ()->assertEquals("ok",response.path("errmsg").toString())
        );
    }

    static List<String> getUserId() throws Exception{
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        User users = mapper.readValue(new File("src/test/resources/wechat/getuserid.yaml"), User.class);
        return users.getUserId();
    }

    /**
     * 更新成员，使用yaml进行数据驱动及并发测试
     */
    @DisplayName("更新成员")
    @Description("测试是否更新成员成功")
    @Story("更新成员测试")
    @ParameterizedTest
    @MethodSource("updateMembers")
//    @Execution(CONCURRENT)
    @Order(3)
    public void testUpdateMembers(HashMap<String,Object> userInfo){
        Response response = given().log().all()
                .contentType(ContentType.JSON)
                .queryParam("access_token",accessToken)
                .body(userInfo)
                .when()
                .post("https://qyapi.weixin.qq.com/cgi-bin/user/update")
                .then().log().all().extract().response();
        assertEquals("0",response.path("errcode").toString());

    }

    static List<HashMap<String,Object>> updateMembers() throws Exception{
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        TypeReference<List<HashMap<String,Object>>> typeReference = new TypeReference<List<HashMap<String,Object>>>(){};
        List<HashMap<String, Object>> userInfo = mapper.readValue(new File("src/test/resources/wechat/updateusers.yaml"), typeReference);
        return userInfo;
    }

    /**
     * 删除成员
     */
    @DisplayName("删除成员")
    @Description("测试是否删除成员成功")
    @Story("删除成员测试")
    @Test
    @Order(4)
    public void testDelMembers(){
        Response response = given().log().all()
                .contentType(ContentType.JSON)
                .queryParam("access_token",accessToken)
                .queryParam("userid","lisi")
                .when()
                .get("https://qyapi.weixin.qq.com/cgi-bin/user/delete")
                .then().log().all().extract().response();
        assertEquals("0",response.path("errcode").toString());

    }

}
